package com.boot.service;

import com.aliyun.oss.OSS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Service
public class AliyunOSSService {
    @Autowired
    private OSS ossClient;

    @Value("${aliyun.oss.bucket-name}")
    private String bucketName;

    @Value("${aliyun.oss.endpoint}")
    private String endpoint; // Add this to get the endpoint from config

    public String upload(MultipartFile file, String filePath, String fileName) throws IOException {
        String path = filePath + "/" + fileName;
        // Upload file stream
        ossClient.putObject(bucketName, path, file.getInputStream());

        // Generate access URL
        return "https://" + bucketName + "." + endpoint.replace("http://", "").replace("https://", "") + "/" + path;
    }

    public void delete(String filePath) {
        ossClient.deleteObject(bucketName, filePath);
    }
}